import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import resetFormData from "./functions/resetFormData";
import saveNewData from "./functions/saveNewData";
import {formatDate} from "../../../../../../../../hrpub/common/utils/utils";

const {NCModal, NCButton} = base;
const {Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter} = NCModal;

class AddNewModal extends Component {
    constructor(props) {
        super(props);
        this.closeModal = this.closeModal.bind(this);
        this.beSure = this.beSure.bind(this);
        this.initModal = this.initModal.bind(this);
        this.onBeforeFormEvent = this.onBeforeFormEvent.bind(this);
        this.onAfterFormEvent = this.onAfterFormEvent.bind(this);
    }

    initModal() {
        const {form} = this.props;
        this.param_str = '';
        form.setFormStatus('outbatchadd', 'add');
        let flag = form.getFormItemsVisible('outbatchadd', 'issynwork');
        if (flag) {
            form.setFormItemsValue('outbatchadd', {
                'transfer_date': {
                    value: formatDate(new Date()),
                    display: null
                },
                'issynwork': {
                    value: true
                }
            })
        } else {
            form.setFormItemsValue('outbatchadd', {
                'transfer_date': {
                    value: formatDate(new Date()),
                    display: null
                }
            })
        }
    }

    onBeforeFormEvent(props, moduleId, key, value, index) {
        if (key === 'pk_psnjob' || key === 'newpk_org' || key === 'newpk_dept' || key === 'pk_businesstype') {
            let that = this;
            let meta = that.props.meta.getMeta();
            const {orgVal, form, isInJob, func_type} = that.props;
            switch (key) {
                case  'pk_psnjob':
                    let psnJobItem = meta[moduleId].items.find(item => item.attrcode === key);
                    psnJobItem.isMultiSelectedEnabled = true;
                    psnJobItem.queryCondition = function () {
                        return {
                            "pk_org": orgVal.refpk,//主组织
                            "istran": func_type === 1, // true:在职  false:离职
                            "isIncludeKeyPsnGrp": func_type === 1,// true:在职  false:离职
                            "GridRefActionExt": "nccloud.web.hrtrn.pub.sqlbuilder.PsntransterAddPsnRefSqlBuilder"
                        }
                    };
                    break;
                case 'newpk_org':
                    let orgItem = meta[moduleId].items.find(item => item.attrcode === key);
                    orgItem.queryCondition = function () {
                        return {
                            "pk_org": orgVal.refpk,//主组织
                            "istran": func_type === 1, // true:在职  false:离职
                            "TreeRefActionExt": "nccloud.web.hrtrn.pub.sqlbuilder.PsntransterAddOrgRefSqlBuilder"
                        }
                    };
                    break;
                case 'newpk_dept':
                    const newOrg = form.getFormItemsValue('outbatchadd', 'newpk_org');
                    if (newOrg.value) {
                        let deptItem = meta[moduleId].items.find(item => item.attrcode === key);
                        deptItem.queryCondition = function () {
                            return {
                                "pk_org": newOrg.value,//组织参照选择的值  不是主组织
                                "TreeRefActionExt": "nccloud.web.hrtrn.pub.sqlbuilder.PsntransterAddDeptRefSqlBuilder"
                            }
                        };
                    }
                    break;
                case 'pk_businesstype':
                    let businesstypeItem = meta[moduleId].items.find(item => item.attrcode === key);
                    businesstypeItem.queryCondition = function () {
                        return {
                            trnsEvent: 3
                        }
                    };
                    break;
                default:
                    break;
            }

            that.props.meta.setMeta(meta);
        }
        return true;
    }

    onAfterFormEvent(props, moduleId, key, value, oldValue) {
        if (key === 'pk_psnjob') {
            this.param_str = value.value;
            if (!value[0]) return;
            resetFormData.call(this, value[0].refpk)
        } else if(key === 'newpk_org') {
            // 切换目标组织后 清除目标部门的值
            props.form.setFormItemsValue(moduleId,{newpk_dept: {display: null, value: null}})
        }
    }

    beSure() {
        const flag = this.props.form.isCheckNow('outbatchadd');
        if (!flag) return;
        saveNewData.call(this, this.param_str)
    }

    closeModal() {
        this.props.onClose && this.props.onClose();
        this.props.form.EmptyAllFormValue('outbatchadd');
    }

    render() {
        const {visible, form} = this.props;
        return (
            <NCModal
                show={visible}
                size="lg"
                onHide={this.closeModal}
                onEntered={this.initModal}
                backdrop={'static'}
            >
                <NCModalHeader
                    closeButton={true}
                >
                    {this.props.json['hi6007-000135']}{/* 国际化处理： 新增人员*/}
                </NCModalHeader>
                <NCModalBody>
                    {
                        form.createForm('outbatchadd', {
                            onBeforeEvent: this.onBeforeFormEvent,
                            onAfterEvent: this.onAfterFormEvent
                        })
                    }
                </NCModalBody>
                <NCModalFooter>
                    <NCButton colors="primary" onClick={this.beSure}>
                        {this.props.json['hi6007-000174']}{/* 国际化处理： 确认*/}
                    </NCButton>
                    <NCButton shape="border" onClick={this.closeModal}>
                        {this.props.json['hi6007-000005']}{/* 国际化处理： 取消*/}
                    </NCButton>
                </NCModalFooter>
            </NCModal>
        );
    }
}

export default AddNewModal;
